<?php

/**
 * 领域控制操作脚本
 */
class SceneController
{
    protected $client;

    public function __construct($host = "127.0.0.1", $port = 80)
    {
        if (!($this->client = new Swoole\Coroutine\Http\Client($host, $port))) {
            echo "ip地址错误！";
            exit;
        }
    }
    public function hanlder()
    {
        while (true) {
            $scenes = $this->sceneIndex();
            fwrite(STDOUT, "当前服务器所有场景详情：" . PHP_EOL);
            foreach ($scenes as $scene) {
                $scene['state'] = $scene['status'] ? "启用" : "停用";
                fwrite(STDOUT, "########  场景：{$scene['scene']}:{$scene['name']}  状态：{$scene['state']} ########" . PHP_EOL);
            }
            fwrite(
                STDOUT,
                "请选择要执行的操作：" . PHP_EOL .
                    "1、增加新场景到当前服务器；" . PHP_EOL .
                    "2、更新当前服务器场景状态；" . PHP_EOL .
                    "3、结束管理程序；" . PHP_EOL
            );
            $opt = trim(fgets(STDIN));

            switch ($opt) {
                case 1:
                    $this->sceneStore();
                    break;
                case 2:
                    $this->sceneUpdate($scenes);
                    break;
                case 3:
                    return -1;
                default:
                    break;
            }
        }
    }

    private function sceneIndex()
    {
        $response = $this->coroutine('/huisheng_api/scenes/index');
        if ($response['code'] != 200) {
            throw new \Exception($response['error']);
        }
        return $response['data'];
    }

    private function sceneStore()
    {
        $param = array();
        fwrite(STDOUT, "请输入新增场景的拼音：" . PHP_EOL);
        $param['scene'] = trim(fgets(STDIN));
        fwrite(STDOUT, "请输入新增场景的中文：" . PHP_EOL);
        $param['name'] = trim(fgets(STDIN));
        $response = $this->coroutine('/huisheng_api/scenes/store', 'post', $param);
        if ($response['code'] != 200) {
            throw new \Exception($response['error']);
        }
        fwrite(STDOUT, "添加成功！" . PHP_EOL);
        return;
    }

    private function sceneUpdate($scenes)
    {
        fwrite(STDOUT, "请选择要修改的场景：" . PHP_EOL);
        foreach ($scenes as $key => $scene) {
            $key++;
            fwrite(STDOUT, "{$key}、{$scene['name']}" . PHP_EOL);
        }
        $num = trim(fgets(STDIN)) - 1;
        if (!isset($scenes[$num])) {
            fwrite(STDOUT, "修改的场景不存在，请检查输入！" . PHP_EOL);
            return $this->sceneUpdate($scenes);
        }
        $param = $scenes[$num];
        foreach ($param as $attr => &$prop) {
            switch ($attr) {
                case 'scene':
                    fwrite(STDOUT, "当前场景拼音：{$prop}，请输入要更新的场景拼音(不修改直接回车跳过)：" . PHP_EOL);
                    $in = trim(fgets(STDIN));
                    $prop = $in != NULL ? $in : $prop;
                    break;
                case 'name':
                    fwrite(STDOUT, "当前场景中文：{$prop}, 请输入要更新的场景中文(不修改直接回车跳过)：" . PHP_EOL);
                    $in = trim(fgets(STDIN));
                    $prop = $in != NULL ? $in : $prop;
                    break;
                case 'status':
                    $state = $prop ? "启用" : "停用";
                    fwrite(STDOUT, "当前场景状态：{$state}, 请选择更改状态(不修改直接回车跳过)：" . PHP_EOL . "1、启用" . PHP_EOL . "0、停用" . PHP_EOL);
                    $in = trim(fgets(STDIN));
                    var_dump($in === "0");
                    $prop = $in !== NULL ? ($in === '1' || $in === '0' ? $in : $prop) : $prop;
                    break;
                default:
                    break;
            }
        }

        $response = $this->coroutine('/huisheng_api/scenes/update', 'post', $param);
        if ($response['code'] != 200) {
            throw new \Exception($response['error']);
        }
        fwrite(STDOUT, "更新成功！" . PHP_EOL);

        return;
    }

    private function coroutine($url, $type = 'get', $param = [])
    {
        $client = $this->client;
        switch ($type) {
            case 'get':
                $client->get($url);
                break;
            case 'post':
                $client->post($url, $param);
                break;
            default:
                throw new \Exception("请求类型错误");
                break;
        }

        if ($client->statusCode != 200) {
            throw new \Exception("客户端连接服务器失败，请检查服务器配置！");
        }
        $result = json_decode($client->body, true);
        return $result;
    }
}




Co\run(function () use ($argc, $argv) {
    if ($argc != 1 && $argc != 2) {
        echo "Usage:\n";
        echo "php {$argv[0]} [<host>:<port>] \n";
        return -1;
    }
    if (isset($argv[1])) {
        $host = explode(":", $argv[1])[0];
        $port = explode(":", $argv[1])[1];
        $scene = new SceneController($host, $port);
    } else {
        $scene = new SceneController();
    }
    $scene->hanlder();
});
